//百度地图全局变量
var markerArr=[],
	title=[],
	address=[],
	telephone=[],
	s=[],
	stringArr=[];
var Search = {
	markersInfo: [],
	metaMarkers: []
};
var radius='2000';

function MAP(){
	var map = new BMap.Map("allmap");            // 创建Map实例
	var mPoint = new BMap.Point(125.31364243,43.89833761);  
	map.enableScrollWheelZoom();
	map.centerAndZoom(mPoint,15);
	map.disableDragging();     //禁止拖拽
    // map.addOverlay(mPoint);
    var local = new BMap.LocalSearch(map, {
	    renderOptions: {
	        map: map,  
	        autoViewport: true,  
	        selectFirstResult: false  
	    },
	    onSearchComplete: function(results){
	        //可以得到搜索结果且搜索结果不为空 
	        if(local.getStatus() == BMAP_STATUS_SUCCESS){
	        	searchCompletes(results)
	        }else{
	        	$('.result').html('').append('<div>暂无数据</div>');
	        }
	    },
	    // pageCapacity: 8  //查询条数
	});
    local.searchNearby('学校',mPoint,radius); 
    $('.hui').html('').html('2公里内的学校')

	//距离保留一位小数点
	function returnFloat1(value) {
		value = Math.round(parseFloat(value) * 10) / 10;
		if(value.toString().indexOf(".") < 0) {
			value = value.toString() + "";
		}
		return value;
	}

	bindTabClickEvent();
	var index = 1;
	//获取返回的数据并渲染到页面上
	function searchCompletes(results) {
		 //从1开始
		var s = [];
		var stringArr = [];
		var num = results.getCurrentNumPois(); //获取结果数
		Search.markersInfo = []; //制空
		Search.metaMarkers = [];
		map.clearOverlays(); //	清除地图上所有覆盖物
		myMarker();	//调用楼盘标记
		var theMiniObj;
		for(var i = 0; i < num; i++) {
			var result = results.getPoi(i);
			var dis = returnFloat1(parseInt(map.getDistance(mPoint, result.point)));
			Search.markersInfo.push({
				'lng': result.point.lng,
				'lat': result.point.lat,
				'subject': result.title,
				'address': result.address,
				'dis': dis
			});
			stringArr.push(dis);
		}
		//遍历结果并添加到页面
		for(var j = 0; j < Search.markersInfo.length; j++) {
			var result = Search.markersInfo[j];
			var letter=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
			if(j<4){
				s.push('<p><span><i class="icon">'+letter[j]+'</i>'+result.subject+'</span><span>'+result.dis+'米</span></p>')
			}else if(j==4){
				s.push('<p><span><i class="icon">'+letter[j]+'</i>'+result.subject+'</span><span>'+result.dis+'米</span></p>');
				index++;
			}else if(j==Search.markersInfo.length-1){
				s.push('<p class="showinfo'+(index-1)+'" style="display:none"><span><i class="icon">'+letter[j]+'</i>'+result.subject+'</span><span>'+result.dis+'米</span></p><p class="more"><span></span><span style="color: blue">更多<i class="iconfont">&#xe60a;</i></span></p><p class="retract" style="display:none;"><span></span><span style="color: blue;">收起<i class="iconfont">&#xe631;</i></span></p>')
			}else{
				s.push('<p class="showinfo'+(index-1)+'" style="display:none"><span><i class="icon">'+letter[j]+'</i>'+result.subject+'</span><span>'+result.dis+'米</span></p>')
			}
		}
		console.log(s)
		$('.itmetotal').append(Search.markersInfo.length+'个'+results.keyword)
		$('.result').append('<div><p class="houseName">'+results.keyword+'</p>'+(s.length==0?"暂无数据":s).join("")+'</div>');
		more();
	}

	//楼盘标记
	function myMarker(){
    	var marker = new BMap.Marker(mPoint);  // 创建标注
		map.addOverlay(marker);              // 将标注添加到地图中
		var label = new BMap.Label("长春绿园3333",{
			offset:new BMap.Size(20,-10)
		});
		marker.setLabel(label);
    }
    //点击底部导航切换搜索内容
	function bindTabClickEvent() {
		$('.myMap ol li').click(function(){
			var tabClass=$(this).attr('indexid');
			switch(tabClass){
				case "map-tab1":
					index=1;	//初始化隐藏的class名
					$('.itmetotal').html('');
					$('.result').html('');
					local.searchNearby("学校", mPoint, radius);
					$('.hui').html('').html('2公里内的学校');
					break;
				case "map-tab2":
					index=1;	//初始化隐藏的class名
					jiaotong();
					$('.hui').html('').html('2公里内的交通');
					break;
				case "map-tab3":
					index=1;	//初始化隐藏的class名
					$('.itmetotal').html('');
					$('.result').html('');
					local.searchNearby("医院", mPoint, radius);
					$('.hui').html('').html('2公里内的医院')
					break;
				case "map-tab4":
					index=1;	//初始化隐藏的class名
					business();
					$('.hui').html('').html('2公里内的商业设施')
					break;
				case "map-tab5":
					index=1;	//初始化隐藏的class名
					itme();
					$('.hui').html('').html('2公里内的楼盘')
					break;
			}
			$(this).addClass("fontColor").siblings().removeClass("fontColor");
		})
	}
	//交通
	function jiaotong(){
		// local.searchNearby("交通", mPoint, radius);
		$('.itmetotal').html('');
		$('.result').html('');
		var arr=["地铁","公交","停车场","加油站"];
		for(var i=0;i<arr.length;i++){
			local.searchNearby(arr[i], mPoint, radius);
		}
	}
	//商业
	function business(){
		$('.itmetotal').html('');
		$('.result').html('');
		var arr=['娱乐','超市','餐饮','银行','公园']
		for(var i=0;i<arr.length;i++){
			local.searchNearby(arr[i], mPoint, radius);
		}
	}
	//楼盘
	function itme(){
		$('.itmetotal').html('');
		$('.result').html('');
		var arr=['住宅','商铺','别墅','公寓','写字楼']
		for(var i=0;i<arr.length;i++){
			local.searchNearby(arr[i], mPoint, radius);
		}
	}
}

function more(){
	$('.more').unbind('click');
	$('.more').each(function(index){
		$(this).click(function(){
			var str='showinfo'+(index+1);
			$('.'+str).show();
			$('.retract').eq(index).show();
			$(this).hide();
			$(this).parent().siblings().hide();
			up();
		})
	})
}

function up(){
	$('.retract').each(function(index){
		$(this).click(function(){
			var str='showinfo'+(index+1);
			$('.'+str).hide();
			$(this).hide();
			$(this).parent().siblings().show();
			$('.more').eq(index).show();
			more()
		})
	})
}